package vn.huync.smsbackupandrestore;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import vn.huync.smsbackupandrestore.others.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.nch.smsbackupandrestore.R;

public class DeleteActivity extends Activity {
	
	MyApp myApp;
	String[] listFile;
	ListView lvFile;
	Button bDelete;
	ListFileCheckableAdapter adapter;
	

	boolean[] isCheckedArray;
	int numberChecked;
	
	private static final int DIALOG_ASK_DELETE = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_delete);
		
		myApp = (MyApp)getApplicationContext();
		lvFile = (ListView)findViewById(R.id.l_delete);
		
		
		//get list file in folder app
		File dir = new File(myApp.appFolderPath);
		listFile = dir.list(new FilenameFilter() {
			
			public boolean accept(File dir, String filename) {
				if(filename.endsWith(".xml"))
					return true;
				else return false;
			}
		});
		isCheckedArray = new boolean[listFile.length];
		numberChecked = 0;
		adapter = new ListFileCheckableAdapter(getApplicationContext(), listFile);
		lvFile.setAdapter(adapter);
		
		bDelete = (Button)findViewById(R.id.b_delete_now);
		bDelete.setOnClickListener(onClickListener);
		
	}
	
	protected android.app.Dialog onCreateDialog(int id) 
	{
		switch (id) {
		case DIALOG_ASK_DELETE:
			AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(DeleteActivity.this);
			dialogBuilder.setTitle(R.string.ask_delete_file_title);
			dialogBuilder.setMessage(R.string.ask_delete_file_message);
			dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
			dialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					final ArrayList<String> fileCheckedArrayList = new ArrayList<String>();
					for(int i = 0; i < isCheckedArray.length; i++){
						if(isCheckedArray[i] == true){
							fileCheckedArrayList.add(listFile[i]);
							
							// delete file in sdcard
							File file = new File(myApp.appFolderPath + listFile[i]);
							file.delete();
						}
					}
					
					Thread tDelete = new Thread(new Runnable() {
						
						public void run() {
							
							for(String fileToDelete : fileCheckedArrayList){
									
									Log.i("", "Begin delete file: "+ fileToDelete);
									
									// delete file in cloud
									Utils.DeleteFileDropbox(myApp, fileToDelete);
									Utils.deleteFileDrive(myApp, fileToDelete, myApp.service);
									
									Log.i("", "End delete file: "+ fileToDelete);
//								}
							}
							
							runOnUiThread(new Runnable() {
								public void run() {
									Toast.makeText(getApplicationContext(), getString(R.string.delete_success), Toast.LENGTH_SHORT).show();
								}
							});
						}
					});
					tDelete.start();
					
					//reset list file
					File dir = new File(myApp.appFolderPath);
					listFile = dir.list(new FilenameFilter() {
						
						public boolean accept(File dir, String filename) {
							if(filename.endsWith(".xml"))
								return true;
							else return false;
						}
					});
					isCheckedArray = new boolean[listFile.length];
					numberChecked = 0;
					
					adapter = new ListFileCheckableAdapter(getApplicationContext(), listFile);
					lvFile.setAdapter(adapter);
				}
			});
			
			dialogBuilder.setNegativeButton(android.R.string.cancel, null);
			return dialogBuilder.create();

		default:
			break;
		}
		return null;
	}
	
	View.OnClickListener onClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			if(v.getId() == R.id.b_delete_now){
				if(numberChecked > 0)
					showDialog(DIALOG_ASK_DELETE);
				else Utils.ShowToast(getApplicationContext(), R.string.please_select_file_to_delete);
			}
		};
	};
	
	class ListFileCheckableAdapter extends ArrayAdapter<String>{
		Context context;
		String[] lFiles;
		
		public ListFileCheckableAdapter(Context context, String[] lFiles) {
			super(context, 0, lFiles);
			this.context = context;
			this.lFiles = lFiles;
		}
		@Override
		public boolean areAllItemsEnabled() {
			if(bDelete != null)
				return bDelete.isEnabled();
			return true;
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if(row == null){
				LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
				row = inflater.inflate(R.layout.row_text_checkbox, null);
				
				ImageView iconImageView = (ImageView)row.findViewById(R.id.row_rt_icon);
				iconImageView.setImageResource(R.drawable.view_icon);
			}
			
			
			row.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					isCheckedArray[position] = !isCheckedArray[position];
					if(isCheckedArray[position]) {
						numberChecked++;
					}
					else {
						numberChecked--;
					}
					adapter.notifyDataSetChanged();
				}
			});
			
			TextView tText = (TextView) row.findViewById(R.id.row_rt_text);
			CheckBox cCheck = (CheckBox)row.findViewById(R.id.row_rt_checkbox);
			
			String fileName = lFiles[position];
			tText.setText(fileName);
			
			cCheck.setChecked(isCheckedArray[position]);
			
			return row;
		}
	}
}
